1244D - Paint the Tree - CodeForces Solution


brute force constructive algorithms dp graphs implementation trees *1800

Please click on ads to support us..

C++ Code:

#define _CRT_SECURE_NO_WARNINGS



#include <map>

#include <set>

#include <stack>

#include <ctime>

#include <cmath>

#include <queue>

#include <cstdio>

#include <cctype>

#include <vector>

#include <bitset>

#include <cstdlib>

#include <cstring>

#include <cassert>

#include <fstream>

#include <iostream>

#include <algorithm>



using namespace std;



typedef long long LL;



inline char gc() {

	static const LL L = 233333;

	static char sxd[L], *sss = sxd, *ttt = sxd;

	if (sss == ttt) {

		ttt = (sss = sxd) + fread(sxd, 1, L, stdin);

		if (sss == ttt) {

			return EOF;

		}

	}

	return *sss++;

}



#ifdef ONLINE_JUDGE

#define dd c = gc()

#else

#define dd c = getchar()

#endif

inline char readalpha() {

	char dd;

	for (; !isalpha(c); dd);

	return c;

}



inline char readchar() {

	char dd;

	for (; c == ' '; dd);

	return c;

}



template <class T>

inline bool read(T& x) {

	bool flg = false;

	char dd;

	x = 0;

	for (; !isdigit(c); dd) {

		if (c == '-') {

			flg = true;

		} else if(c == EOF) {

			return false;

		}

	}

	for (; isdigit(c); dd) {

		x = (x << 1) + (x << 3) + (c ^ 48);

	}

	if (flg) {

		x = -x;

	}

	return true;

}

#undef dd



template <class T>

inline void write(T x) {

	if (x < 0) {

		putchar('-');

		x = -x;

	}

	if (x < 10) {

		putchar(x | 48);

		return;

	}

	write(x / 10);

	putchar((x % 10) | 48);

}



typedef long long LL;



typedef long long LL;



const int maxn = 100005;



int n;

int c[maxn][3];

LL f[maxn][3][3];

int g[maxn][3][3];



struct Edge {

	int to, nxt;

} e[maxn << 1];



int first[maxn];

int du[maxn];

int A[maxn];



inline void add_edge(int from, int to) {

	static int cnt = 0;

	du[from]++, du[to]++;

	e[++cnt].nxt = first[from];

	first[from] = cnt;

	e[cnt].to = to;

	e[++cnt].nxt = first[to];

	first[to] = cnt;

	e[cnt].to = from;

}



int bh[maxn];



inline void dfs(int now, int fa) {

	static int cnt = 0;

	bh[++cnt] = now;

	for (int i = first[now]; i; i = e[i].nxt) {

		int to = e[i].to;

		if (to != fa) {

			dfs(to, now);

		}

	}

}



int main() {

	read(n);

	for (int i = 0; i < 3; ++i) {

		for (int j = 1; j <= n; ++j) {

			read(c[j][i]);

		}

	}

	for (int i = 1; i < n; ++i) {

		int x, y;

		read(x), read(y);

		add_edge(x, y);

	}

	int dd = 0;

	for (int i = 1; i <= n; ++i) {

		if (du[i] > 2) {

			puts("-1");

			return 0;

		} else if (du[i] == 1) {

			dd = i;

		}

	}

	dfs(dd, 0);

//	for (int i = 1; i <= n; ++i) {

//		cout << bh[i] << ' ';

//	}

//	cout << endl;

	memset(f, 0x3f, sizeof(f));

	for (int i = 0; i < 3; ++i) {

		for (int j = 0; j < 3; ++j) {

			if (i != j) {

				f[2][j][i] = c[bh[1]][i] + c[bh[2]][j];

				g[2][j][i] = 0;

			}

		}

	}

//	for (int i = 0; i < 3; ++i) {

//		for (int j = 0; j < 3; ++j) {

//			cout << f[2][i][j] << ' ';

//		}

//		cout << endl;

//	}

//	cout << endl;

	for (int i = 3; i <= n; ++i) {

		for (int j = 0; j < 3; ++j) {

			for (int k1 = 0; k1 < 3; ++k1) {

				for (int k2 = 0; k2 < 3; ++k2) {

					if (j != k1 && k1 != k2 && k2 != j) {

						if (f[i][j][k1] > f[i - 1][k1][k2] + c[bh[i]][j]) {

							f[i][j][k1] = f[i - 1][k1][k2] + c[bh[i]][j];

							g[i][j][k1] = k2;

						}

					}

				}

//				cout << f[i][j][k1] << ' ';

			}

//			cout << endl;

		}

//		cout << endl;

	}

	LL a1 = 0, a2 = 0, ans = 0x3f3f3f3f3f3f3f3fll;

	for (int i = 0; i < 3; ++i) {

		for (int j = 0; j < 3; ++j) {

			if (ans > f[n][i][j]) {

				a1 = i, a2 = j;

				ans = f[n][i][j];

			}

		}

	}

	printf("%lld\n", ans);

	for (int i = n; i; --i) {

		A[bh[i]] = a1;

		int T = g[i][a1][a2];

		a1 = a2;

		a2 = T;

	}

	for (int i = 1; i <= n; ++i) {

		printf("%d ", A[i] + 1);

	}

	return 0;

}


Comments

Submit
0 Comments
More Questions

52A - 123-sequence
1543A - Exciting Bets
1714D - Color with Occurrences
215B - Olympic Medal
1445A - Array Rearrangment
1351A - A+B (Trial Problem)
935B - Fafa and the Gates
1291A - Even But Not Even
1269A - Equation
441A - Valera and Antique Items
1702C - Train and Queries
816B - Karen and Coffee
838D - Airplane Arrangements
148B - Escape
847G - University Classes
1110A - Parity
1215B - The Number of Products
604C - Alternative Thinking
1204C - Anna Svyatoslav and Maps
322A - Ciel and Dancing
1689B - Mystic Permutation
1711B - Party
1702D - Not a Cheap String
1714F - Build a Tree and That Is It
1703F - Yet Another Problem About Pairs Satisfying an Inequality
610A - Pasha and Stick
1200A - Hotelier
1091A - New Year and the Christmas Ornament
1352B - Same Parity Summands
1102A - Integer Sequence Dividing